package com.zeei.bfs.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zeei.bfs.vo.DataHeader;
import com.zeei.bfs.service.QueryDataService;

import com.zeei.common.annotation.SysLog;
import com.zeei.common.controller.BasicController;
import com.zeei.common.enums.LogType;
import com.zeei.common.utils.SessionInfoUtil;
import com.zeei.common.vo.ResultVO;

@Controller
@RequestMapping(value = "/bfs/dataquery")
public class QueryDataController extends BasicController {
	private static final Logger logger = LoggerFactory.getLogger(QueryDataController.class);

	@Resource
	private QueryDataService dataQueryService;

	/** 数据表头接口 */
	@ResponseBody
	@RequestMapping(value = "dataheader", method = RequestMethod.POST)
	public ResultVO<DataHeader> dataRealTimeHeader(String dataQuery) {
		ResultVO<DataHeader> result = new ResultVO<DataHeader>();
		try {
			String sql = dataQuery.toLowerCase();
			// 过滤掉的sql关键字，可以手动添加
			String badStr = "exec |execute |insert |delete |update |drop |mid|master|truncate|"
					+ "sitename|net user|xp_cmdshell|;|-|+|,|create |"
					+ "table |grant |use |column_name |" + "information_schema.columns|table_schema |*|"
					+ ";|-|--|+|,|//|/|#";
			String[] badStrs = badStr.split("\\|");
			for (int i = 0; i < badStrs.length; i++) {
				if (sql.indexOf(badStrs[i]) >= 0) {
					result.setResult(false);
					result.setMessage("不能输入delete,update,create等此类特殊字段！");
					return result;

				} 
			}
			List<DataHeader> headerList = getHeader(dataQuery);
			result.setRows(headerList);
			result.setTotal(headerList.size());
		} catch (Exception e) {
			Integer userId = SessionInfoUtil.getSessionUserId();
			logger.error("用户{}|操作{}|异常", userId, "查询数据表头接口");
			logger.error("异常明细:", e);
		}
		return result;
	}

	/** 数据列表接口 */
	@ResponseBody
	@RequestMapping(value = "datalist", method = RequestMethod.POST)
	@SysLog(text = "查询数据", type = LogType.View)
	public ResultVO<Map<String, Object>> realDataList(String dataQuery) {
		List<Map<String, Object>> resultList = dataQueryService.queryData(dataQuery);
		ResultVO<Map<String, Object>> result = new ResultVO<Map<String, Object>>();
		result.setRows(resultList);
		result.setTotal(resultList.size());
		return result;
	}
	public List<DataHeader> getHeader(String dataQuery) {
		List<DataHeader> headerList = new ArrayList<DataHeader>();
		List<Map<String, Object>> map = dataQueryService.queryData(dataQuery);
		if (map!=null && map.size() > 0 && map.get(0)!=null) {
			for (String key : map.get(0).keySet()) {
				DataHeader header = new DataHeader();
				header.setTitle(key);
				header.setField(key);
				headerList.add(header);
			}			
		}
		return headerList;
		
	}
}
